﻿// <copyright file="IdentityCheck.cs" company="">
// Copyright (c) 2013 All Rights Reserved
// </copyright>
// <author></author>
// <date>1/24/2013 6:43:10 AM</date>
// <summary>Implements the IdentityCheck Workflow Activity.</summary>
namespace Xrm.Solutions.FrameworkSample.Workflow
{
    using System;
    using System.Activities;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Xrm.Sdk.Query;
    using Xrm.Solutions.FrameworkSample.Common.Entities;
    using Xrm.Solutions.FrameworkSample.Common;

    public sealed class IdentityCheck : CodeActivity
    {

        #region Input Parameters

        [Input("First Name")]
        [RequiredArgument()]
        public InArgument<string> FirstName { get; set; }

        [Input("Last Name")]
        [RequiredArgument()]
        public InArgument<string> LastName { get; set; }

        [Input("Date of Birth")]
        [RequiredArgument()]
        public InArgument<DateTime> DateOfBirth { get; set; }

        #endregion

        #region Output Parameters

        [Output("Result")]
        public OutArgument<string> Result { get; set; }

        #endregion

        /// <summary>
        /// Executes the workflow activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        protected override void Execute(CodeActivityContext executionContext)
        {
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();

            if (tracingService == null)
                throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");

            tracingService.Trace("Entered IdentityCheck.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
                executionContext.ActivityInstanceId,
                executionContext.WorkflowInstanceId);

            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

            if (context == null)
                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");

            tracingService.Trace("IdentityCheck.Execute(), Correlation Id: {0}, Initiating User: {1}",
                context.CorrelationId,
                context.InitiatingUserId);

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            try
            {
                IIdentityCheckService creditCheckService = new IdentityCheckService();
                int result = creditCheckService.RunCheck(FirstName.Get(executionContext), LastName.Get(executionContext), DateOfBirth.Get(executionContext));
                
                if (0 <= result && result <= 40)
                    Result.Set(executionContext, "No way!");
                else if (41 <= result && result <= 60)
                    Result.Set(executionContext, "Mmm... try again");
                else
                    Result.Set(executionContext, "Good guy");
                
            }
            catch (FaultException<OrganizationServiceFault> e)
            {
                tracingService.Trace("Exception: {0}", e.ToString());
                throw;
            }

            tracingService.Trace("Exiting IdentityCheck.Execute(), Correlation Id: {0}", context.CorrelationId);
        }
    }
}